package com.zlt.util;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class AlipayUtil {

    private static String url;
    private static String appid;
    private static String privateKey;
    private static String publicKey;
    private static String notifyUrl;
    private static String returnUrl;

    static {
        // 加载配置文件
        InputStream is = SqlUtil.class.getClassLoader().getResourceAsStream("pay.properties");
        Properties properties = new Properties();
        try {
            // 读取配置文件中的内容
            properties.load(is);
            url=properties.getProperty("url");
            appid=properties.getProperty("appid");
            privateKey=properties.getProperty("privateKey");
            publicKey=properties.getProperty("publicKey");
            notifyUrl=properties.getProperty("notifyUrl");
            returnUrl=properties.getProperty("returnUrl");

        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public static String pay(String id, String price, String title ){
        //默认给我们创建支付宝客户端
        AlipayClient alipayClient = new DefaultAlipayClient(url,appid,privateKey,"json","UTf-8",publicKey,"RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl(notifyUrl); //支付成功后  异步通知 请求我们服务器 然后告知我们这次请求是否成功
        //这个地址需要一个公网ip  这样在外网访问这个地址  就能访问到
        //这个请求是支付宝发送的
        request.setReturnUrl(returnUrl); //字符成功后返回到哪里
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);//订单编号
        bizContent.put("total_amount", price);//订单价格
        bizContent.put("subject", title);//订单标题
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY"); //产品编码  默认就是这个

        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = null;
        String form = null;  //响应的是一个表单
        try {
            response = alipayClient.pageExecute(request);  //这里请求成功 获取响应
            //可以从响应中获取返回的表单
            form = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
//            System.out.println("调用失败");
        }
        System.out.println(form);
        return form;
    }



    
    public static String query(String id){
        AlipayClient alipayClient = new DefaultAlipayClient(url,appid,privateKey,"json","UTf-8",publicKey,"RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body = null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }

        System.out.println("body:"+body);
        return body;
    }

}
